package com.liu;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;

public class ScriptTest {

    public static void main(String[] args) {
        try {
            // 命令行数组创建
            LinkedList<String> command = new LinkedList<>();
            command.add("D:\\soft\\Python\\Python312\\python.exe");
            command.add("D:\\soft\\Python\\statistics\\connect_statistics.py");
//            command.add("D:\\soft\\Python\\statistics\\day_statistics.py");
//            command.add("D:\\soft\\Python\\statistics\\test.py");
            command.add("788177040812675072");
//            command.add("2025-05-16 00:00:00");
//            command.add("2025-05-16 09:57:07");

//            command.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis() - (24 * 60 * 60 * 1000)));
//            command.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            // 创建ProcessBuilder
            ProcessBuilder pb = new ProcessBuilder(command);

            // 启动进程
            Process process = pb.start();

            // 获取Python脚本的输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println("Python输出: " + line);
            }

            // 等待进程结束并获取退出码
            int exitCode = process.waitFor();
            System.out.println("Python脚本退出码: " + exitCode);

            if(exitCode != 0) {
                try(InputStream errorStream = process.getErrorStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(errorStream))){
                    String error;
                    while((error = bufferedReader.readLine())!= null){
                        System.out.println("发生错误 " +error);
                    }
                }
            }

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
